package com.xiaoke.liantongdemo.xml;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.xiaoke.liantongdemo.bean.BusinessInput;
import com.xiaoke.liantongdemo.bean.BusinessInputs;
import com.xiaoke.liantongdemo.bean.BusinessPage;

public class BusinessInputsHandler extends DefaultHandler {
	
	private BusinessInputs businessInputs;
	private ArrayList<BusinessPage> businessPageList;
	private BusinessPage businessPage;
	private ArrayList<BusinessInput> businessInputList;
	private BusinessInput businessInput;
	
	
	private static final String ELEMENT_FILE = "file";
	private static final String ELEMENT_PAGE = "page";
	private static final String ELEMENT_FIELD = "field";
	private static final String ATTR_NUMBER = "number";
	private static final String ATTR_NAME = "name";
	private static final String ATTR_TYPE = "type";
	private static final String ATTR_DESC = "desc";
	private static final String ATTR_VALUE = "value";
	private static final String ATTR_MAX_LENGTH = "max_length";
	
	private static final String TYPE_TEXT = "textfield";
	private static final String TYPE_CHOICE = "choicefield";
	private static final String TYPE_SIGN = "signature";
	
	private static final String VALUES_SPLIT = "-";
	
	
	public BusinessInputs getBusinessInputs(){
		return businessInputs;
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		super.endElement(uri, localName, qName);
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if(localName.equals(ELEMENT_FILE)){
			businessInputs = new BusinessInputs();
			businessPageList = new ArrayList<BusinessPage>();
			businessInputs.setBusinessPageList(businessPageList);
		}else if(localName.equals(ELEMENT_PAGE)){
			businessPage = new BusinessPage();
			businessPage.setNumber(Integer.parseInt(attributes.getValue(ATTR_NUMBER)));
			businessInputList = new ArrayList<BusinessInput>();
			businessPage.setBusinessInputList(businessInputList);
			businessPageList.add(businessPage);
		}else if(localName.equals(ELEMENT_FIELD)){
			businessInput = new BusinessInput();
			businessInput.setName(attributes.getValue(ATTR_NAME));
			businessInput.setType(getType(attributes.getValue(ATTR_TYPE)));
			businessInput.setDesc((attributes.getValue(ATTR_DESC)));
			if(businessInput.getType() == BusinessInput.Type.TEXT){
//				businessInput.setMaxLength((Integer.parseInt(attributes.getValue(ATTR_MAX_LENGTH))));
			}
			if(businessInput.getType() == BusinessInput.Type.CHOICE){
				businessInput.setValues((getValues((attributes.getValue(ATTR_VALUE)))));
			}
			businessInputList.add(businessInput);
		}
	}
	
	private int getType(String typeStr){
		if(TYPE_TEXT.equals(typeStr)){
			return BusinessInput.Type.TEXT;
		}else if(TYPE_CHOICE.equals(typeStr)){
			return BusinessInput.Type.CHOICE;
		}else if(TYPE_SIGN.equals(typeStr)){
			return BusinessInput.Type.SIGN;
		}else{
			return 0;
		}
	}
	
	private String[] getValues(String valuesString){
		return valuesString.split(VALUES_SPLIT);
	}

}
